In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
In [5]:
def softmax(scores):
exps = np.exp(scores)
return exps / exps.sum()
In [53]:
a = np.array([2., -1, 1])
plt.stem(a)
plt.xlim([-1, len(a)]);
In [54]:
a2 = softmax(a)
plt.stem(a2)
plt.xlim([-1, len(a)])
print("Softmax probabilities sum to", np.sum(a2))
In [24]:
prototypes_raw = np.array([
[3, 1, 1],
[1, 3, 1],
[1, 1, 3]], dtype=float)
prototype_magnitudes = np.linalg.norm(prototypes_raw, axis=1, keepdims=True)
prototypes = prototypes_raw / prototype_magnitudes
prototypes
Out[24]:
In [25]:
def similarities(vec, prototypes):
return np.dot(prototypes, vec)
similarities([1, 2, 3], prototypes)
similarities([3, 2, 1], prototypes)
Out[25]:
In [26]:
softmax(similarities([1, 2, 3], prototypes))
Out[26]:
In [27]:
def relu(vector):
return np.maximum(vector, 0)
x = np.linspace(-2, 2, 100)
plt.plot(x, relu(x))
Out[27]:
In [28]:
def tanh(vector):
return np.tanh(vector)
plt.plot(x, tanh(x));
Out[28]:
In [ ]:
from scipy.misc import imsave, fromimage, toimage
from PIL import Image, ImageOps
In [31]:
DATA = '../data/'
WIDTH = HEIGHT = 224
def load_and_crop_image(filename, target_size):
return ImageOps.fit(Image.open(filename), target_size)
In [43]:
gatos = np.array([fromimage(load_and_crop_image(DATA+'/gatos/cat.{:04}.jpg'.format(i), (WIDTH, HEIGHT))) for i in range(1,100)])
gatos_nocolor = np.mean(gatos, axis=3)
In [44]:
img = gatos_nocolor[0]
toimage(img)
Out[44]:
In [45]:
import scipy.signal
Cambia filt
hasta que la imagen parece como lo siguiente
In [49]:
filt = np.array([
[1, 0, -1],
[1, 0, -1],
[1, 0, -1]
])
convolved = scipy.signal.convolve2d(img, filt)
toimage(convolved)
Out[49]:
In [49]:
filt = np.array([
[1, 0, -1],
[1, 0, -1],
[1, 0, -1]
])
convolved = scipy.signal.convolve2d(img, filt)
toimage(convolved)
Out[49]:
In [ ]: